﻿hpxAdminApp.controller('informationController', function ($scope, $rootScope, $state, $stateParams, $window, API_URL, NgTableParams, portalInformationService, portalInformationTypeService) {
    portalInformationTypeService.get($stateParams.type).then(function (data) {
        $scope.typeName = data.information_type_name;
    })

    var emptyEntity = {
        information_type_id: $stateParams.type
    };
    var newEntity = angular.copy(emptyEntity);

    $scope.filter = {};

    $scope.tableParams = new NgTableParams({}, {
        getData: function (params) {
            //获取所有的发布信息类型
            return portalInformationService.query(params, $scope.filter.keyword, $stateParams.type, null, null).then(function (data) {
                $scope.first = $scope.getFirst(params);
                return data;
            });
        }
    });
    //刷新
    $scope.reflash = function () {
        $scope.tableParams.reload();
    }
    
    $scope.edit = function (item) {
        if (item == null) {         //弹出新建发布信息类型窗口
            $scope.model = newEntity;
        }
        else {          //弹出修改发布信息类型窗口
            portalInformationService.get(item.id).then(function (data) {
                $scope.model = data;
            });
        }
        $('#modal-edit').modal('show');
    };

    $scope.save = function () {
        if ($scope.model.id == null) {           //新建一条发布信息类型信息
            portalInformationService.add($scope.model).then(function (data) {
                $scope.tableParams.reload();
                angular.copy(emptyEntity, newEntity);
                $scope.editForm.$setPristine();
                $('#modal-edit').modal('hide');
            });
        }
        else {              //修改一条发布信息类型数据
            portalInformationService.update($scope.model).then(function (data) {
                $scope.tableParams.reload();
                $scope.editForm.$setPristine();
                $('#modal-edit').modal('hide');
            });
        }

    };
    //删除一条发布信息
    $scope.remove = function (data) {
        if (confirm('确定要删除该信息吗？ ')) {
            portalInformationService.remove(data.id).then(function (data) {
                $scope.tableParams.reload();
            });
        }
    };
});